# -*- coding: utf-8 -*-
__author__ = "Ypf"
_date_ = "2019/6/20  13:14"


from  common.mssql_helper import g_msql_inst

from collections import defaultdict

from common.utils import format_float, create_uuid



def filter_sb(dic,filter_list):
    new_dic = {}
    a=[0]
    for i in filter_list:
        if dic[i]:
            new_dic[i] = dic[i]
        else:
            new_dic[i]=a
    return new_dic
class AccountMgr(object):

    @staticmethod
    def get_id_name(sql):
        '''
        获取id和名字的对比字典
        :param sql: 封装的sql语句
        :return:
        '''
        res_list = g_msql_inst.fetch_all(sql)
        # print(res_list)
        # return res_list
        # select  khqc, idowner from  account
        # 假设按照idowner进行group by
        name_dict = defaultdict(dict)
        for (ID, sName) in res_list:
            name_dict[ID] = sName
        return name_dict


    @staticmethod
    def get_account_groupby(sql,name_dic):
        '''
        获取一般客户经理销售积分
        :param sql: 封装的sql语句
        :return:
        '''
        res_list = g_msql_inst.fetch_all(sql)
        #print(res_list)
        #return res_list
        # select  khqc, idowner from  account
        # 假设按照idowner进行group by
        res_gy_dict = defaultdict(list)
        #[(`1, 2, 3),(4, 5, 6)]  --> {2:[1, 3], 5:}
        for (idowner,xsjf ) in res_list:
            idowner= name_dic.get(idowner)
            res_gy_dict[idowner].append(format_float(xsjf))
        return res_gy_dict


    @staticmethod
    def get_kehujixiao(sql,name_dic):
        '''
        获取客户经理绩效
        :param sql: 封装的sql语句
        :return:
        '''
        res_list = g_msql_inst.fetch_all(sql)
        #print(res_list)
        #return res_list
        # select  khqc, idowner from  account
        # 假设按照idowner进行group by
        khjf_dic = defaultdict(list)
        for (idowner, jxjf ) in res_list:
            idowner = name_dic.get(idowner)
            khjf_dic[idowner].append((format_float(jxjf)))
        return khjf_dic

    @staticmethod
    def store_khjlqb_statics(bgtime,edtime, input_dic,db_insert_sql):
        '''
        保存数据库信息
        :param report_list: 统计出的报表结果数据cpjlrckh
        :return:
        '''
        value_tuple_list = []
        for key in input_dic:
            id = create_uuid()
            dic_val=input_dic[key]
            value_tuple = (id,key,dic_val[0],dic_val[1],dic_val[2],dic_val[3],dic_val[4],dic_val[5],dic_val[6],dic_val[7],bgtime,edtime)
            excuete_sql = db_insert_sql % value_tuple
            print(excuete_sql)
            g_msql_inst.db_excuete(excuete_sql)
            value_tuple_list.append(value_tuple)

    @staticmethod
    def store_khjldd_statics(bgtime, edtime, input_dic, db_insert_sql):
        '''
        保存数据库信息
        :param report_list: 统计出的报表结果数据cpjlrckh
        :return:
        '''
        value_tuple_list = []
        for key in input_dic:
            id = create_uuid()
            dic_val = input_dic[key]
            value_tuple = (
            id, key, dic_val[0], dic_val[1], dic_val[2], dic_val[3], dic_val[4], dic_val[5], dic_val[6], dic_val[7],dic_val[8],dic_val[9],
            bgtime, edtime)
            excuete_sql = db_insert_sql % value_tuple
            print(excuete_sql)
            g_msql_inst.db_excuete(excuete_sql)
            value_tuple_list.append(value_tuple)